from commons.entity.po.user import User
from flask_sqlalchemy import SQLAlchemy
from commons.entity.po.model import db


class AdvanceApproval(db.Model):
    __tablename__ = 'advance_approval'

    advance_approval_id = db.Column(
        db.Integer, primary_key=True, autoincrement=True, info='财务人员审批预支款记录')
    advance_id = db.Column(db.ForeignKey(
        'travel.travel_id'), index=True, info='预支款编号')
    payment = db.Column(db.Float, info='预支款金额')
    finance_id = db.Column(db.ForeignKey('user.user_id'),
                           index=True, info='财务人员编号')
    date = db.Column(db.Date, info='审批时间')

    advance = db.relationship('Travel', primaryjoin='AdvanceApproval.advance_id == Travel.travel_id',
                              backref='advance_approvals')
    finance = db.relationship('User', primaryjoin='AdvanceApproval.finance_id == User.user_id',
                              backref='advance_approvals')

    def to_dict(self):
        return {
            'advance_approval_id': self.advance_approval_id,
            'advance_id': self.advance_id,
            'payment': self.payment,
            'finance_id': self.finance_id,
            'date': self.date.isoformat() if self.date else None,
        }

    def to_dict_2(self):
        return {
            'advanceApprovalId': self.advance_approval_id,
            'advanceId': self.advance_id,
            'payment': self.payment,
            'financeId': self.finance_id,
            'date': self.date.isoformat() if self.date else None,
        }
